<template>
  <div class="article-detail">
    <HmHeader showBack @clickLeft="leftFn">
      <template> 面经详情 </template>
      <template #nav-right> ... </template>
    </HmHeader>
    <!-- <nav class="nav">
      <div class="back" @click="$router.back()">
        &lt;
        <i class="iconfont icon-arrow-left"></i>
      </div>
      <span class="title">面经详情</span>
    </nav> -->

    <div class="detail-wrap">
      <header class="header">
        <h1 class="title">{{ articleDetail.stem }}</h1>

        <div class="author-info">
          <img :src="articleDetail.avatar" alt="" class="avatar" />
          <div class="info">
            <span class="name">{{ articleDetail.creator }}</span>
            <div class="meta">
              <span>{{ articleDetail.createdAt }}</span>
              <span class="divider">|</span>
              <span>{{ articleDetail.views }} 浏览</span>
              <span class="divider">|</span>
              <span>{{ articleDetail.likeCount }} 点赞</span>
            </div>
          </div>
        </div>
      </header>
      <div class="icon-wrap">
        <span
          class="likeIcon"
          @click="fn(1)"
          :style="{
            backgroundColor: articleDetail.likeFlag ? 'orange' : '#fff',
          }"
          ><van-icon
            name="like-o"
            size="20"
            :color="articleDetail.likeFlag ? 'white' : '#000'"
        /></span>
        <span
          @click="fn(2)"
          class="starIcon"
          :style="{
            backgroundColor: articleDetail.collectFlag ? 'orange' : '#fff',
          }"
          ><van-icon
            name="star-o"
            size="20"
            :color="articleDetail.collectFlag ? 'white' : '#000'"
        /></span>
      </div>

      <main class="content" v-html="articleDetail.content"></main>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import HmHeader from "@/components/HmHeader.vue";
export default {
  name: "HmDetail",
  components: {
    HmHeader,
  },
  data() {
    return {
      articleDetail: {},
      msg: "",
    };
  },
  created() {
    this.getArticleDetail();
  },
  methods: {
    async getArticleDetail() {
      if (!this.$route.query.id) {
        this.$router.back();
        return;
      }
      const res = await axios.get(
        `http://interview-api-t.itheima.net/h5/interview/show?id=${this.$route.query.id}`,
        {
          headers: {
            Authorization: `Bearer ${localStorage.getItem("token")}`,
          },
        }
      );
      this.articleDetail = res.data.data;
    },
    leftFn() {
      this.$router.back();
    },
    fn(num) {
      console.log(num);

      //http://interview-api-t.itheima.net/h5/interview/opt 携带token发送post请求
      axios
        .post(
          "http://interview-api-t.itheima.net/h5/interview/opt",
          {
            id: this.$route.query.id,
            optType: num,
          },
          {
            headers: {
              Authorization: `Bearer ${localStorage.getItem("token")}`,
            },
          }
        )
        .then(() => {
          //重新获取文章详情
          this.getArticleDetail();
          if (num === 1) {
            if (this.articleDetail.likeFlag) {
              this.msg = "取消点赞";
            } else {
              this.msg = "点赞成功";
            }
          }
          if (num === 2) {
            if (this.articleDetail.collectFlag) {
              this.msg = "取消收藏";
            } else {
              this.msg = "收藏成功";
            }
          }
          this.$toast.success(this.msg);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.article-detail {
  background: #f5f5f5;
  min-height: 100vh;

  //   .nav {
  //     position: fixed;
  //     top: 0;
  //     left: 0;
  //     right: 0;
  //     height: 46px;
  //     background: #fff;
  //     display: flex;
  //     align-items: center;
  //     padding: 0 16px;
  //     font-size: 16px;
  //     color: #333;
  //     border-bottom: 1px solid #f5f5f5;
  //     z-index: 100;

  //     .back {
  //       padding: 5px;
  //       margin-right: 10px;
  //       cursor: pointer;

  //       i {
  //         font-size: 20px;
  //       }
  //     }

  //     .title {
  //       font-weight: 500;
  //     }
  //   }

  .detail-wrap {
    padding: 10px 16px 20px;

    .header {
      padding: 15px;
      margin-bottom: 10px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

      .title {
        font-size: 18px;
        font-weight: 500;
        color: #333;
        line-height: 1.5;
        margin: 0 0 15px;
      }

      .author-info {
        display: flex;
        align-items: center;

        .avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          margin-right: 10px;
        }

        .info {
          flex: 1;

          .name {
            font-size: 14px;
            color: #333;
            margin-bottom: 4px;
            display: block;
          }

          .meta {
            font-size: 12px;
            color: #999;

            .divider {
              margin: 0 6px;
            }
          }
        }
      }
    }

    .content {
      padding: 20px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
      line-height: 1.8;
      color: #333;
      font-size: 15px;
      text-indent: 2em;
    }
    .icon-wrap {
      position: fixed;
      bottom: 100px;
      right: 10px;
      display: flex;
      align-items: center;
      padding: 10px;
      span {
        width: 30px;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        background-color: white;
        border-radius: 50%;
        margin-right: 20px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
      }
    }
  }
}
</style>
